<script>
	import UniSocket from "@/im/uni.socket.js";
	import {
		mapActions,
		mapMutations,
		mapState
	} from 'vuex';
	import {
		socketUrl
	} from '@/utils/index.js';
	export default {
		onLaunch() {
			// debugger
			const updateManager = uni.getUpdateManager()
			updateManager.onCheckForUpdate(function(res) {
				// 请求完新版本信息的回调
				console.log(res.hasUpdate)
			})
			// 下载新版本
			updateManager.onUpdateReady(function() {
				uni.showModal({
					title: '更新提示',
					content: '新版本已经准备好，是否重启应用？',
					success(res) {
						if (res.confirm) {
							// 重启应用
							updateManager.applyUpdate()
						}
					}
				})
			})
			// 新版本下载失败
			updateManager.onUpdateFailed(function(res) {
				// 新的版本下载失败
				uni.showModal({
					title: '已经有新版本了',
					content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开',
				})

			})
			//判断是否显示未读消息红点
			this.getMsgMap();
			uni.$on('onSocketMessage', (data) => {
				this.handleMessage(data);
				this.get_chat_contact_list();

				uni.$emit('refreshChat');
			});
			uni.$on('onSocketOpen', () => {
				this.chatRegiste();
				this.pushLoginMessage();
			});
		},
		globalData: {
			chatLogin: false
		},
		data() {
			return {
				sysNum: 0,
				commitNum: 0,
			}
		},
		computed: {
			...mapState('userStore', ['userInfo']),
		},
		methods: {
			...mapMutations('chatStore', ['handleMessage']),
			...mapMutations('userStore', ['saveUserInfo']),
			...mapActions('chatStore', ['pushLoginMessage', 'get_chat_contact_list']),
			//获取未读信息
			getMsgMap() {
				this.$request.get({
					url: 'sysMessage/getMsgMap',
				}).then(info => {
					console.log('info', info)
					for (let k of info) {
						if (k.label == '通知') {
							this.sysNum = k.value ? k.value : 0
						}
						if (k.label == '评论') {
							this.commitNum = k.value ? k.value : 0
						}
					}
					//显示小圆点
					if (parseFloat(this.sysNum) !== 0 || parseFloat(this.commitNum) !== 0) {
						uni.showTabBarRedDot({
							index: 2
						});
					}
				}, res => {});
			},
			connectWS() {
				try {
					const userInfo = uni.getStorageSync('userInfo');
					let _self = this;
					if (userInfo && this.globalData.chatLogin === false) {
						this.globalData.socket = new UniSocket({
							url: socketUrl + "?userId=" + userInfo.userId,
							timeout: 1 * 60 * 1000, // 心跳时间间隔
							isSendHeart: true, // 是否发送心跳
							heartData: "ping", // 心跳数据
							isReconnection: true, // 是否断线重连
							reConnectTime: 5000, // 断线重连检测时间间隔
							params: { // 发送消息时如果时json则自动加上组合里面参数
								// token: this.$store.state.user.TOKEN
							},
							debug: true, // debug
							onSocketOpen: header => {
								_self.globalData.chatLogin = true;
								uni.$emit('onSocketOpen');
							},
							onSocketMessage: res => {
								console.log('onSocketMessage', res);
								uni.$emit('onSocketMessage', res.data);
							},
							onSocketError: res => {
								console.log('onSocketError', res);
							},
							onSocketClose: res => {
								this.globalData.chatLogin = false;
							}
						});
						this.globalData.socket.initSocket();
						console.log('新生成的socket实例', this.globalData.socket)
					}

				} catch (e) {
					//TODO handle the exception
				}

			},
			chatRegiste() {
				if (this.userInfo) {
					this.globalData.socket.sendSocketMessage({
						userId: this.userInfo.userId,
						type: "REGISTER"
					});
				}
			},
			closeSocket() {
				this.globalData.socket && this.globalData.socket.closeSocket();
			}
		}
	};
</script>

<style>
	/*每个页面公共css */
	@import './common/uni.css';
	@import './common/iconfont.css';
	@import './common/common.css';

	view {
		box-sizing: border-box;
	}
</style>
